<template>
  <div class="edit-container">
    <el-col :span="12">
      <el-form :model="dataForm" label-width="120px" :rules="dataFormRules" ref="dataForm" :size="size" label-position="right">
        <el-form-item label="角色" prop="name">
          <el-input ref="name" v-model="dataForm.name" auto-complete="off" />
        </el-form-item>
        <el-form-item label="顺序" prop="sort">
          <el-input type="number" v-model.number="dataForm.sort" auto-complete="off" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio v-for="item in statusList" :key="item.value" :label="item.value">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 6}" v-model="dataForm.remark" auto-complete="off" maxlength="500" show-word-limit />
        </el-form-item>
        <el-form-item label="权限" prop="menuIds">
          <el-tree ref="menuIds" :data="menuTree" :default-checked-keys="dataForm.menuIds" node-key="id" :props="defaultProps" show-checkbox default-expand-all check-strictly />
        </el-form-item>
        <el-form-item>
          <el-button :size="size" @click.native="$router.go(-1)">取消</el-button>
          <el-button :size="size" type="primary" @click.native="save" :loading="loading">保存</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      size: 'mini',
      loading: false,
      menuTree: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      dataFormRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      // 新增编辑界面数据
      dataForm: {
        id: 0,
        name: '',
        sort: 999,
        status: '0',
        remark: '',
        menuIds: []
      }
    }
  },
  methods: {
    // 加载菜单树
    loadMenuTree () {
      this.$api.menu.getTree().then(res => {
        if (res.code === 0) {
          this.menuTree = res.data
        }
      })
    },
    loadData () {
      if (this.$route.query.id !== undefined) {
        this.dataForm.id = this.$route.query.id
        this.$api.role.get({ id: this.dataForm.id }).then((res) => {
          this.dataForm = res.data
        })
      }
    },
    save () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.loading = true
          let params = Object.assign({}, this.dataForm)
          params.menuIds = this.$refs.menuIds.getCheckedKeys()
          if (params.menuIds.length === 0) {
            params.menuIds.push(-1)
          }
          this.$api.role.save(params).then((res) => {
            this.loading = false
            if (res.code === 0) {
              this.$message({ message: '操作成功', type: 'success' })
              this.$router.push(this.getCurrentParentPath() + '/list')
            } else {
              this.$message({ message: '操作失败, ' + res.message, type: 'error' })
            }
          })
        }
      })
    }
  },
  mounted () {
    this.loadMenuTree()
    this.loadData()
    this.$refs.name.focus()
  },
  computed: {
    ...mapState({
      statusList: state => state.app.typeList.sys_normal_disable
    })
  }
}
</script>
